package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.jd.pojo.GoodsOrderShipping;
import com.gxa.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class OrderShippingServiceImpl implements OrderShippingService {

    @Autowired
    private OrderShippingMapper orderShippingMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<GoodsOrderShipping> findAll() {
        return orderShippingMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<GoodsOrderShipping> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<GoodsOrderShipping> orderShippings = (Page<GoodsOrderShipping>) orderShippingMapper.selectAll();
        return new PageResult<GoodsOrderShipping>(orderShippings.getTotal(), orderShippings.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<GoodsOrderShipping> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return orderShippingMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<GoodsOrderShipping> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<GoodsOrderShipping> orderShippings = (Page<GoodsOrderShipping>) orderShippingMapper.selectByExample(example);
        return new PageResult<GoodsOrderShipping>(orderShippings.getTotal(), orderShippings.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param
     * @return
     */
    @Override
    public GoodsOrderShipping findById(String orderId) {
        return orderShippingMapper.selectByPrimaryKey(orderId);
    }

    /**
     * 新增
     *
     * @param orderShipping
     */
    @Override
    public void add(GoodsOrderShipping orderShipping) {
        orderShippingMapper.insert(orderShipping);
    }

    /**
     * 修改
     *
     * @param orderShipping
     */
    @Override
    public void update(GoodsOrderShipping orderShipping) {
        orderShippingMapper.updateByPrimaryKeySelective(orderShipping);
    }

    /**
     * 删除
     *
     * @param
     */
    @Override
    public void delete(String orderId) {
        orderShippingMapper.deleteByPrimaryKey(orderId);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(GoodsOrderShipping.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // order_id
            if (searchMap.get("orderId") != null && !"".equals(searchMap.get("orderId"))) {
                criteria.andLike("orderId", "%" + searchMap.get("orderId") + "%");
            }
            // receiver_address
            if (searchMap.get("receiverAddress") != null && !"".equals(searchMap.get("receiverAddress"))) {
                criteria.andLike("receiverAddress", "%" + searchMap.get("receiverAddress") + "%");
            }
            // receiver_city
            if (searchMap.get("receiverCity") != null && !"".equals(searchMap.get("receiverCity"))) {
                criteria.andLike("receiverCity", "%" + searchMap.get("receiverCity") + "%");
            }
            // receiver_name
            if (searchMap.get("receiverName") != null && !"".equals(searchMap.get("receiverName"))) {
                criteria.andLike("receiverName", "%" + searchMap.get("receiverName") + "%");
            }
            // receiver_phone
            if (searchMap.get("receiverPhone") != null && !"".equals(searchMap.get("receiverPhone"))) {
                criteria.andLike("receiverPhone", "%" + searchMap.get("receiverPhone") + "%");
            }
            // receiver_province
            if (searchMap.get("receiverProvince") != null && !"".equals(searchMap.get("receiverProvince"))) {
                criteria.andLike("receiverProvince", "%" + searchMap.get("receiverProvince") + "%");
            }
            // receiver_town
            if (searchMap.get("receiverTown") != null && !"".equals(searchMap.get("receiverTown"))) {
                criteria.andLike("receiverTown", "%" + searchMap.get("receiverTown") + "%");
            }


        }
        return example;
    }

}
